package com.jd.pet.service.impl;

import com.jd.basic.jwt.UserInfo;
import com.jd.basic.utils.AjaxResult;
import com.jd.basic.utils.BaiduAuditUtils;
import com.jd.basic.utils.LoginContext;
import com.jd.org.domain.Employee;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.pet.domain.Pet;
import com.jd.pet.domain.PetDetail;
import com.jd.pet.domain.PetOnlineAuditLog;
import com.jd.pet.mapper.PetDetailMapper;
import com.jd.pet.mapper.PetMapper;
import com.jd.pet.mapper.PetOnlineAuditLogMapper;
import com.jd.pet.service.IPetService;
import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.user.domain.Logininfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jd
 * @since 2023-03-13
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetOnlineAuditLogMapper mapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void add(Pet pet) {
        // 在新增宠物的时候我们需要新增店铺id和创建时间
        pet.setCreatetime(new Date());
        pet.setState(0);
//        // 添加店铺id
//        // 获取登录人信息
//        Logininfo loginInfo = LoginContext.getLoginInfo();
//        // 根据登录人信息查询店铺id
//        Employee employee = employeeMapper.findByLogininfoId(loginInfo.getId());
//        if (null != employee) {
//            pet.setShopId(employee.getShopId());
//        }
        pet.setShopId(27L);
        // 保存宠物表
        super.add(pet);
        // 保存宠物详情表
        PetDetail petDetail = pet.getDetail();
        petDetail.setPetId(pet.getId());
        petDetailMapper.add(petDetail);
    }

    @Override
    public void update(Pet pet) {

        super.update(pet);
    }
    // 宠物下架
    @Override
    public void offSale(List<Long> ids) {
        // 将宠物的状态修改为下架
        // 添加宠物下架时间
        petMapper.batchUpdateById(ids);

    }
    // 宠物上架
    @Override
    public void onSale(List<Long> ids) {
        // 校验参数
        // 先判断该宠物是否处于下架状态(在sql语句中直接判断，state=0）
        List<Pet> pets =petMapper.findByIds(ids);
        // 新建petIdList批量修改上架状态
        List<Long> petIdList = new ArrayList<>();
        //
        List<PetOnlineAuditLog> petAuditLogs = new ArrayList<>();
        // 非空判断
        if (null == pets){
            return;
        }
        // 百度AI校验
        pets.forEach(pet -> {
            // 新建一个字符串 用来拼接百度校验结果
            StringBuffer stringBuffer = new StringBuffer();
            // 先判断该宠物是否处于下架状态
            // 先检验名称
            if (!BaiduAuditUtils.TextCensor(pet.getName())){
                stringBuffer.append("宠物名称不合规，宠物id为：").append(pet.getId());
            }
            // 在校验图片
            String resources = pet.getResources();
            if (StringUtils.isNotEmpty(resources)){
                String[] resource = resources.split(",");
                for (int i = 0; i < resource.length; i++) {
                    if (!BaiduAuditUtils.ImgCensor(resource[i])){
                        stringBuffer.append("宠物图片不符合规范，宠物Id为：").append(pet.getId()).append("图片为：")
                                .append(resource[i]);
                    }
                }
            }
            // 添加宠物审核日志(审核不通过添加)
            if (stringBuffer.length()>0){
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setAuditTime(new Date());
                // 当前管理员id
                log.setAuditId(99L);
                log.setNote(stringBuffer.toString());
                log.setPetId(pet.getId());
                log.setState(0);
//                mapper.add(log);
                petAuditLogs.add(log);
                return;
            }
                petIdList.add(pet.getId());
        });
        // 批量修改状态
        if (!CollectionUtils.isEmpty(petIdList)){
            petMapper.batchUpdate(petIdList);
        }
        // 批量添加日志
        if (!CollectionUtils.isEmpty(petAuditLogs)){
            mapper.batchAdd(petAuditLogs);
        }

    }



}
